package com.java.business.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;

import cn.hutool.core.util.StrUtil;
import com.java.business.entity.CourseCategory;
import com.java.business.entity.CourseContent;
import com.java.business.service.CourseCategoryService;
import com.java.common.dto.SortDto;
import com.java.common.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.java.business.entity.Course;
import com.java.business.service.CourseService;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;


/**
 * 课程
 *
 * @author jl
 * @since 2020-10-07 10:31:35
 */
@RestController
@RequestMapping("admin/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseCategoryService courseCategoryService;

    /**
     * 带条件分页查询课程
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = courseService.queryPage(params);

        return R.ok(page);
    }


    /**
     * 根据主键查询课程
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        Course course = courseService.getById(id);

        return R.ok(course);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public R save(@RequestBody Course course) {
        if (StrUtil.isNotBlank(course.getId())) {
            courseService.updateById(course);
        } else {
            course.setId(UuidUtil.getShortUuid());
            courseService.save(course);
        }
        return R.ok(course);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        courseService.removeById(id);
        return R.ok();
    }

    /**
     * 查找课程下所有分类
     */
    @PostMapping("/list-category/{courseId}")
    public R listCategory(@PathVariable("courseId") String courseId) {
        List<CourseCategory> courseCategories = courseCategoryService.listCategory(courseId);
        return R.ok(courseCategories);
    }

    @GetMapping("/find-content/{id}")
    public R findContent(@PathVariable("id") String id) {
        CourseContent content = courseService.findContent(id);
        return R.ok(content);
    }

    @PostMapping("/save-content")
    public R saveContent(@RequestBody CourseContent courseContent) {
        courseService.saveContent(courseContent);
        return R.ok();
    }

    /**
     * 更新排序
     */
    @PostMapping("/sort")
    public R sort(@RequestBody SortDto sortDto) {
        courseService.sort(sortDto);
        return R.ok();
    }
}
